package com.ai.controller.security;


import com.ai.annotation.EnterPathParamLog;
import com.ai.common.CommonResponse;
import com.ai.entity.security.SysPermission;
import com.ai.entity.security.page.SysPermissionPage;
import com.ai.model.param.SysPermissionParam;
import com.ai.service.security.ISysPermissionService;
import com.ai.common.vaild.AddGroup;
import com.ai.common.vaild.UpdateGroup;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 权限表
 * @author chen
 * @since 2023-04-04 19:46:06
 */
@RestController
@RequestMapping("/sys-permission")
public class SysPermissionController {


    @Resource
    private ISysPermissionService sysPermissionService;

    @ApiOperation("分页查询")
    @PostMapping("list")
    @EnterPathParamLog
    public IPage<SysPermission> list(@RequestBody SysPermissionPage page) {

        // 根据创建时间排序
//        QueryWrapper<SysPermission> queryWrapper = page.getQuery(new QueryWrapper<>()).orderByDesc("create_time");
        QueryWrapper<SysPermission> queryWrapper = page.getQuery(new QueryWrapper<>());
        SysPermissionPage ipage = sysPermissionService.page(page, queryWrapper);
        return ipage;
    }

    @ApiOperation("分页查询")
    @PostMapping("permissionList/{roleId}")
    @EnterPathParamLog
    public CommonResponse permissionList(@PathVariable(name= "roleId") String roleId) {
        return sysPermissionService.permissionList(roleId);
    }


    @ApiOperation("分页查询")
    @PostMapping("permissionList")
    @EnterPathParamLog
    public CommonResponse permissionList() {
        return sysPermissionService.permissionList();
    }

    @ApiOperation("查询指定权限")
    @PostMapping("selectById/{id}")
    @EnterPathParamLog
    public CommonResponse<Object> selectById(@PathVariable(name = "id") Integer id) {
        SysPermission serviceById = sysPermissionService.getById(id);
        return CommonResponse.ok("查询成功").setData(serviceById);
    }


    //新增
    @ApiOperation("新增权限")
    @PostMapping("add")
    @EnterPathParamLog
    public CommonResponse<String> add(@RequestBody @Validated({AddGroup.class}) SysPermissionParam permissionParam) {
        return sysPermissionService.add(permissionParam);
    }

    @ApiOperation("删除权限")
    @PostMapping("delete/{id}")
    @EnterPathParamLog
    public CommonResponse<String> delete(@PathVariable(name = "id") Integer id) {
        boolean b = sysPermissionService.removeById(id);
        if (b){
            return CommonResponse.ok("删除成功");
        }else {
            return CommonResponse.error("删除失败");
        }
    }

    @ApiOperation("更新权限信息")
    @PostMapping("update")
    @EnterPathParamLog
    public CommonResponse<String> delete(@RequestBody @Validated({UpdateGroup.class}) SysPermissionParam permissionParam) {


        return sysPermissionService.updatePermission(permissionParam);
    }

}

